﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunSilverlight.Comm;
using DunSilverlight.Views;
using System.Text;
using System.Windows.Browser;

namespace DunSilverlight
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

       

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            onLoad();
            
        }


        private void onLoad()
        {
            if (AppGlobal.CurrentUser == null)
            {
                LoginForm logFrm = new LoginForm();

                //RadWindow frm = null;

                logFrm.CallBack = () =>
                {
                    if (logFrm.IsSuccess)
                    {
                        AppGlobal.CurrentUser = logFrm.LoginUser;
                        AppGlobal.CurrentRights = logFrm.UserRights;
                        NameBlock.Text = AppGlobal.CurrentUser.Name;
                        contentGrid.Visibility = Visibility.Visible;

                        sy.IsChecked = true;

                        (logFrm.Parent as RadWindow).Close();
                    }
                    else
                    {
                        //如何退出程序？---这里不需要退出程序，（按取消键和登录失败要分别处理，取消键的我已经写了。）
                    }
                };

                //frm.CanClose = false;

                //已经在loginform回调了CallBack
                //Tools.OpenWindow("登录", logFrm, null, logFrm.CallBack);

                Tools.OpenWindow("登录", logFrm, null);
                


                //frm.Close();
            }

            //contentGrid.Visibility = Visibility.Visible;
            //sy.IsChecked = true;
        }

        private void onExit()
        {
            
            AppGlobal.Clear();
            HtmlWindow html = HtmlPage.Window;
            html.Navigate(new Uri("DunSilverlightTestPage.aspx", UriKind.Relative));//相对路
            
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Loger.WriteLog("登录", "");
            Loger.WriteLog("注销", "");
            onExit();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ChangePasswordForm form = new ChangePasswordForm();
            Tools.OpenWindow("修改密码", form, null,480,280, new Action(() =>
            {
                if (form.IsSaveSuccess)
                    onExit();
            }));
        }

        private void RadRadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadRadioButton btn = sender as RadRadioButton;
            if (btn != null && btn.Tag != null && VerifyRight(btn.Tag.ToString()))
            {
                if (btn.Tag.ToString().IndexOf("!") != -1)
                {
                    StackPanel panel = this.FindName(btn.Tag.ToString().Replace("!", "")) as StackPanel;
                    IEnumerable<RadRadioButton> btns = panel.ChildrenOfType<RadRadioButton>();
                    foreach (RadRadioButton bn in btns)
                    {
                        if (bn.IsChecked.Value && bn.Tag != null)
                        {
                            if (bn.Tag.ToString().IndexOf("@") != -1)
                            {
                                Type type=Type.GetType("DunSilverlight.Views."+bn.Tag.ToString().Replace("@",""));
                                Object obj = Activator.CreateInstance(type);
                                Tools.OpenWindow(bn.Content.ToString(), obj, null);
                            }
                            else
                            {
                                contentFrame.Navigate(new Uri("/Views/" + bn.Tag.ToString() + ".xaml", UriKind.Relative));
                            }
                        }
                    }
                }
                else if (btn.Tag.ToString().IndexOf("@") != -1)
                {
                    Type type = Type.GetType("DunSilverlight.Views." + btn.Tag.ToString().Replace("@", ""));
                    Object obj = Activator.CreateInstance(type);
                    Tools.OpenWindow(btn.Content.ToString(), obj, null);
                }
                else
                {
                    contentFrame.Navigate(new Uri("/Views/" + btn.Tag.ToString() + ".xaml", UriKind.Relative));
                }
            }
        }
        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        private bool VerifyRight(string tag)
        {
            if (tag == "@ImportArrearRecord")
            {
                if (AppGlobal.CurrentUser.Organization.Parent == null)
                {
                    Tools.ShowMessage("只有分公司用户才能导入数据!", "", false);
                    return false ;
                }

                if (!RightManagement.VerifyRight(RightManagement.Right_Import_Arrear))
                {
                    Tools.ShowMessage("您没有导入催款信息的权限", "", false);
                    return false;
                }
            }

            if (tag == "@ImportReturnRecord")
            {
                if (AppGlobal.CurrentUser.Organization.Parent == null)
                {
                    Tools.ShowMessage("只有分公司用户才能导入数据!", "", false);
                    return false;
                }

                if (!RightManagement.VerifyRight(RightManagement.Right_Import_Arrear))
                {
                    Tools.ShowMessage("您没有导入回款信息的权限", "", false);
                    return false;
                }
            }
            return true;
        }

    }
    public class TestData
    {
        public string Name { get; set; }
        public DateTime Date
        {
            get;
            set;
        }
        public int Value { get; set; }

        public string Number { get; set; }
    }
}
